package com.weipai.education.controller;

import com.weipai.education.enums.RetCode;
import com.weipai.education.model.api.Response;
import com.weipai.education.service.FileUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: 曹朋飞
 * @Date: 2018/12/10 17:09
 */
@RestController
public class ImgController {

    @Autowired
    private FileUploadService fileUploadService;

    /**
     * 上传图片
     * @param request
     * @param img
     * @return
     */
    @RequestMapping(value = "/img/uploadingImg", method = RequestMethod.POST)
    Response uploadingImg(HttpServletRequest request, MultipartFile img){
        Response response = new Response();
        if(img!=null){
            String result = fileUploadService.saveImgUpload(request, img);
            if (result.equals("formatError")) {
                response.setCode(RetCode.IMG_FORMAT);
            } else if (result.equals("sizeover")) {
                response.setCode(RetCode.IMG_SIZE);
            } else if(result.equals("error")){
                response.setCode(RetCode.IMG_ERROR);
            }else{
                response.setCode(RetCode.SUCCESS);
                response.setData(result);
            }
        }else {
            response.setCode(RetCode.NOT_DATA);
        }
        return response;
    }

}
